#include "chatwindow.h"
#include "ui_chatwindow.h"
#include <QDebug>
#include <QTimer>
#include <qdatetime.h>
#include "QtCore"

chatwindow *chatwindow::mychatwindow=nullptr;
chatwindow::chatwindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::chatwindow)
{
    ui->setupUi(this);
      ui->textBrowser_2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
       ui->textBrowser->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

}

chatwindow::~chatwindow()
{
    delete ui;
}

void chatwindow::on_pushButton_clicked()
{QString name;
   ui->textBrowser->setAlignment(Qt::AlignRight);
      /*ui->textBrowser->setStyleSheet(" color:rgb(55,100,255)");
       QDateTime time = QDateTime::currentDateTime();
       QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
       ui->textBrowser->append(str);*/
Account::getInstance()->getA_Gid_FriendName(myid,name);
       ui->textBrowser->append(name+": "+ui->sendtext->toPlainText());


       emit(sendmsg(friendid ,ui->sendtext->toPlainText()));


      qDebug()<<"id:"<<friendid<<"msg"<<ui->sendtext->toPlainText();

       ui->sendtext->clear();
}
void chatwindow::gethistory(QList<QPair<QString,QString>> list){
 history=list;
 QString name;
 for(int i=0;i<history.size();i++){
     if(history[i].first==myid){
         ui->textBrowser->setAlignment(Qt::AlignRight);
         Account::getInstance()->getA_Gid_FriendName(history[i].first,name);
         ui->textBrowser->append(name+": "+history[i].second);
          ui->textBrowser_2->append(" ");
          ui->textBrowser->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
         ui->textBrowser->moveCursor(QTextCursor::End);
     }
     else{
         Account::getInstance()->getA_Gid_FriendName(history[i].first,name);
         ui->textBrowser_2->append(name+": "+history[i].second);
         ui->textBrowser->append(" ");
         ui->textBrowser_2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
         ui->textBrowser_2->moveCursor(QTextCursor::End);
     }

 }
}
void chatwindow::GetFriendMsg(QString msg){
    QString name;
Account::getInstance()->getA_Gid_FriendName(friendid,name);
 ui->textBrowser_2->append(friendid+": "+msg);
  ui->textBrowser->append(" ");
    ui->textBrowser_2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->textBrowser_2->moveCursor(QTextCursor::End);


}
